//index.js
//获取应用实例
const app = getApp()
const listData = require("../../utils/mock")
Page({
  data: {
    listData,
    currentId: "box0",
    active: 0,
    nodeTops: []
  },
  onLoad(){
    // console.log(window,document) 在微信小程序里面没有win,dom对象
    // document.querySelector(".text") {}
    // document.querySelectorAll("#app") []
    // console.log(this.data.listData)
  },
  onReady(){
    //创建选择器上下文
    var  query = wx.createSelectorQuery();
    var  nodeTops = [];
    query.selectAll(".title").boundingClientRect((nodes)=>{
      nodes.map((res)=>{
        nodeTops.push(Math.round(res.top));
      })
    }).select(".scroll-wrap").boundingClientRect((res)=>{
      nodeTops.push(Math.round(res.height));
      this.setData({nodeTops});
    }).exec();
  },
  setBoxId(e){
    var index = e.target.dataset.index;
    var currentId = this.data.currentId;
    currentId = "box"+index;
    this.setData({currentId});
  },
  getScroll(e){
    var scrollTop = e.detail.scrollTop;
    var nodeTops = this.data.nodeTops;
    // var active = nodeTops.findIndex((item,index)=>{
    //   return scrollTop>=nodeTops[index]&&scrollTop<nodeTops[index+1];
    // })
    for(var i=0;i<nodeTops.length;i++){
      var val1 = nodeTops[i];
      var val2 = nodeTops[i+1];
      if(scrollTop>=val1&&scrollTop<val2){
          this.setData({active:i})
      }
    }
    // console.log(active)
    // this.setData({active})
  },
  getUserInfo: function (e) {
    app.globalData.userInfo = e.detail.userInfo
    this.setData({
      userInfo: e.detail.userInfo,
      hasUserInfo: true
    })
  }
})